/*
 * Copyright 2006 Stefan Arentz <http://stefan.arentz.nl>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package test.services.quote;

import com.polarrose.spring.remoting.rest.annotations.RestUriParam;
import com.polarrose.spring.remoting.rest.annotations.RestService;
import com.polarrose.spring.remoting.rest.annotations.RestAction;
import com.polarrose.spring.remoting.rest.annotations.HttpMethod;
import com.polarrose.spring.remoting.rest.annotations.RestContent;

@RestService(path = "/quoteService", contentType = "text/xml")
public class QuoteRestService
{
    private QuoteService quoteService;

    public void setQuoteService(QuoteService quoteService)
    {
        this.quoteService = quoteService;
    }

    // GET /quoteService/widgets/123 -> -> <QuoteServiceResponse><Status>0</Status><Quote></Quote></QuoteServiceResponse>

    @RestAction(method = HttpMethod.GET, template = "/quotes/{id}")
    QuoteServiceResponse getQuote(@RestUriParam("id") long id)
    {
        Quote quote = quoteService.getQuoteById(id);
        if (quote != null) {
            return new QuoteServiceResponse<Quote>();
        } else {
            return new QuoteServiceResponse<Void>(-1);
        }
    }

    // PUT /quoteService/quotes

    @RestAction(method = HttpMethod.PUT, template = "/quotes")
    QuoteServiceResponse<Quote> createQuote(@RestContent Quote quote)
    {
        return null;
    }

    // POST /quoteService/widgets/123 -> <QuoteServiceResponse><Status>0</Status></QuoteServiceResponse>

    @RestAction(method = HttpMethod.DELETE, template = "/quotes/{id}")
    QuoteServiceResponse deleteQuote(long id)
    {
        Quote quote = quoteService.getQuoteById(id);
        if (quoteService != null) {
            quoteService.deleteQuote(quote);
            return new QuoteServiceResponse<Void>();
        } else {
            return new QuoteServiceResponse<Void>(-1);
        }
    }

    //

    class QuoteServiceResponse<T>
    {
        private final int status;

        public int getStatus()
        {
            return status;
        }

        private final T value;

        public T getValue()
        {
            return value;
        }

        QuoteServiceResponse()
        {
            this.status = 0;
            this.value = null;
        }

        QuoteServiceResponse(int status)
        {
            this.status = status;
            this.value = null;
        }

        QuoteServiceResponse(T value)
        {
            this.status = 0;
            this.value = value;
        }
    }
}
